package com.jvtd.eyl.ui.main.home;


import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.jvtd.eventBus.EventCenter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpFragment;
import com.jvtd.eyl.bean.binding.ClassBean;
import com.jvtd.eyl.bean.binding.PeriodicalBean;
import com.jvtd.eyl.bean.eventBus.MainBarBean;
import com.jvtd.eyl.bean.http.BannerBean;
import com.jvtd.eyl.bean.ui.HomeBean;
import com.jvtd.eyl.constants.EventCode;
import com.jvtd.eyl.databinding.FragmentHomeBinding;
import com.jvtd.eyl.ui.main.home.periodicalInfo.PeriodicalInfoActivity;
import com.jvtd.eyl.ui.main.home.search.SearchActivity;
import com.jvtd.eyl.widget.HomeHeaderView;
import com.jvtd.utils.SmartRefreshUtils;
import com.jvtd.utils.UiUtils;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

/**
 * 精选
 * <p>
 * 作者:chenlei
 * 时间:2018/11/12 3:17 PM
 */
public class HomeFragment extends BaseMvpFragment implements HomeMvpView, OnRefreshListener {
    @Inject
    HomePresenter<HomeMvpView> mPresenter;
    private FragmentHomeBinding mBinding;
    private HomeHeaderView mHeaderView;//列表头部布局
    private List<BannerBean> mBannerBeans;//banner数据源
    private List<HomeBean> mHomeBeans;//界面数据源

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected View getLayout(LayoutInflater inflater, @Nullable ViewGroup container) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onDestroyView() {
        mPresenter.onDetach();
        super.onDestroyView();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mHeaderView != null)
            mHeaderView.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mHeaderView != null)
            mHeaderView.start();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        MainBarBean mainBarBean = new MainBarBean(false, true);
        EventBus.getDefault().post(new EventCenter<>(mainBarBean, EventCode.UPDATE_BAR));
    }

    @Override
    protected void initViewAndData() {
        mPresenter.onAttach(this);

        initToolbar();

        initRefreshLayout();

        initHeaderView();

        initRecyclerView();

        onRefresh(null);
    }

    /**
     * 初始化toolbar
     *
     * @author Chenlei
     * created at 2018/11/13
     **/
    private void initToolbar() {
        setToolbar(mBinding.toolBar.toolBar, false);
        UiUtils.moveStatusBarHeight(mBinding.toolBar.toolBar);
        addDisposable(RxView.clicks(mBinding.toolBar.search).throttleFirst(1,TimeUnit.SECONDS).subscribe(o -> startActivity(SearchActivity.getIntent(mContext))));
    }

    /**
     * 初始化下拉刷新
     *
     * @author Chenlei
     * created at 2018/11/13
     **/
    private void initRefreshLayout() {
        SmartRefreshUtils.initRefresh(mContext, mBinding.refreshLayout, this);
    }

    /**
     * 初始化头布局
     *
     * @author Chenlei
     * created at 2018/11/13
     **/
    private void initHeaderView() {
        mHeaderView = new HomeHeaderView(mContext);
        mHeaderView.setOnBannerListener(position -> {
            BannerBean bannerBean = mBannerBeans.get(position);
            if (bannerBean != null && !TextUtils.isEmpty(bannerBean.getDocument_id())) {
//                showMessage(bannerBean.getDocument_id());
                PeriodicalBean periodicalBean = new PeriodicalBean();
                periodicalBean.setId(bannerBean.getDocument_id());
                periodicalBean.setName(bannerBean.getDocument_name());
                startActivity(PeriodicalInfoActivity.getIntent(mContext, periodicalBean));
            }
        });
    }

    /**
     * 初始化列表
     *
     * @author Chenlei
     * created at 2018/11/13
     **/
    private void initRecyclerView() {
        mBinding.recyclerView.openEmptyView((view, state) -> {
            if (state == JvtdEmptyView.STATE_ERROR)
                onRefresh(null);
        }).setRefreshLayout(mBinding.refreshLayout)
                .addHeaderView(mHeaderView);
    }

    /**
     * 界面接口请求及刷新
     *
     * @param refreshlayout 刷新控件 可传空
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getBannerList();
        mPresenter.getClassList();
    }

    /**
     * 获取banner列表成功方法
     *
     * @param bannerBeans 返回的banner列表数据
     */
    @Override
    public void getBannerListSuccess(List<BannerBean> bannerBeans) {
        mBannerBeans = bannerBeans;
        List<String> imgList = new ArrayList<>();
        for (BannerBean bannerBean : bannerBeans) {
            imgList.add(bannerBean.getImg());
        }
        mHeaderView.updateBanner(imgList);
    }

    /**
     * 获取分类列表成功
     *
     * @param classBeans 分类实体类
     */
    @Override
    public void getClassListSuccess(List<ClassBean> classBeans) {
        mHomeBeans = new ArrayList<>();
        for (ClassBean classBean : classBeans) {
            mHomeBeans.add(new HomeBean(classBean));
        }
        mBinding.recyclerView.setData(mHomeBeans);
        mPresenter.getPeriodicalList();
    }

    /**
     * 获取分类方法失败 调用列表失败方法
     *
     * @author Chenlei
     * created at 2018/11/13
     **/
    @Override
    public void getClassListFailed() {
        mBinding.recyclerView.firstLoadFailed();
    }

    /**
     * 获取期刊列表成功
     *
     * @param periodicalBeans 期刊列表实体
     */
    @Override
    public void getPeriodicalListSuccess(List<PeriodicalBean> periodicalBeans) {
        mHomeBeans.add(new HomeBean());
        for (PeriodicalBean periodicalBean : periodicalBeans.subList(0, Math.min(periodicalBeans.size(), 16))) {
            mHomeBeans.add(new HomeBean(periodicalBean));
        }
        mBinding.recyclerView.setData(mHomeBeans);
    }

    @Override
    public void getPeriodicalListFailed() {

    }
}
